*********************************************************************************************************************
***************  E ***************
**************************************************************************************************************
** title: 		Euroscepticism as a syndrome of stagnation? Regional inequality and trust in the EU 	 	**
** authors:		Sofia Vasilopoulou and Liisa Talving														**
** purpose: 	replication																					**
** date:		September 2023 																				**
** software:	Stata/MP 17.0 																				**	
**************************************************************************************************************


clear all
set more off
use "/Users/Data and do file for replication/Data for replication.dta" //  (update as a function of where you store the replication materials and datasets)



*****Descriptive analysis*****

//Information on the number of regions

distinct nuts2_region

//Descriptive overview of main variables

tab trust_eu if year>2014
tab trust_eu year, m col nofreq
tab country trust_eu if year>2014, m row nofreq 

oneway trust_eu nuts2_region if year>2014, mean
tab trust_eu if nuts2_region=="AT21" & year>2014
tab trust_eu if nuts2_region=="DED" & year>2014
tab trust_eu if nuts2_region=="BG41" & year>2014
tab trust_eu if nuts2_region=="RO41" & year>2014

sum gdp_reg if year>2014
sum gdp_reg if nuts2_region=="AT21" & year>2014
sum gdp_reg if nuts2_region=="DED" & year>2014
sum gdp_reg if nuts2_region=="BG41" & year>2014
sum gdp_reg if nuts2_region=="RO42" & year>2014

sum gdp_reg if year>2014
sum gdp_reg if year==2015
sum gdp_reg if year==2019
tab nuts2_region if gdp_reg==8100
tab year if gdp_reg==8100
tab nuts2_region if gdp_reg==79600
tab year if gdp_reg==79600

// Table B in Appendix

eststo clear
estpost sum trust_eu gdp_reg gdp_reg_ch_5 trust_gov ec_exp attach_cntry lr edu occ age gender gdp_nat if year>2014
esttab using descriptives.rtf, cells("mean(fmt(2)) sd(fmt(2)) min(fmt(1)) max(fmt(0))") nomtitle nonumber replace 



*****Regression analysis*****

//H1: Poor and rich European regions tend to trust the EU more compared to middle-income European regions
//Wealth status measured as continuous (Fig 2, Model 1)

logit trust_eu c.gdp_reg##c.gdp_reg trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
vif, uncentered
margins, at(gdp_reg=(8100(1000)79600)) atmeans post
marginsplot, recast(line) recastci(rarea) ///
	graphregion(color(white) lwidth(large)) ///
	plot1opts(lpattern(solid) lcolor(black) lwidth(medium)) ///
	ciopts(lcolor(gs10) fcolor(gs10)) ///
	title ("") ///
	xtitle ("Regional GDP (PPS per inhabitant)") ///
	ytitle (, margin(medsmall)) ///
	xtitle (, margin(medsmall)) ///
	xlab (8100 28805 79600) ///
	xline (28805, lcolor(red) lpattern(dash)) ///
	addplot(histogram gdp_reg if nonmiss==1 & year>2014, discrete freq color(gs12%25) ylab(0.2(0.2)1,nogrid) xlab(8100 28805 79600) yaxis(2) yscale(alt axis(2)) below legend(off))

logit trust_eu c.gdp_reg##c.gdp_reg trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg=(8100 28805.99 79600)) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]

sum gdp_reg if year==2019
tab nuts2_region if gdp_reg==10200 & year==2019
tab nuts2_region if gdp_reg==79600 & year==2019

//Wealth status measured as groups (Model 2, Fig A in Appendix)

logit trust_eu ib(2).gdp_reg_gr trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_gr=(1 2 3)) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]
marginsplot, recast(scatter) recastci(rcap) ///
	graphregion(color(white) lwidth(large)) ///
	title("") ///
	xtitle("Regional groups based on GDP (PPS per capita)", margin(small)) ///
	xscale(range(0.5 1 2 3 3.5)) ///
	xlab(1 "Poor" 2 "Middle-income" 3 "Wealthy") ///
	ylab(0.3(0.1)0.7,nogrid) ///
	legend(col(1))

//H2: Larger regional economic growth is associated with higher levels of trust in the EU
//Wealth growth measured as continuous (t-5) (Fig 3, Model 3)
	
logit trust_eu c.gdp_reg_ch_5##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_ch_5=(-6600(1000)41100)) atmeans post
marginsplot, recast(line) recastci(rarea) ///
	graphregion(color(white) lwidth(large)) ///
	plot1opts(lpattern(solid) lcolor(black) lwidth(medium)) ///
	ciopts(lcolor(gs10) fcolor(gs10)) ///
	title ("") ///
	xtitle ("Regional GDP; 5-year change") ///
	ytitle (, margin(medsmall)) ///
	xtitle (, margin(medsmall)) ///
	xlab (-6600 3882 41100) ///
	xline (3882, lcolor(red) lpattern(dash)) ///
	addplot(histogram gdp_reg_ch_5 if nonmiss==1 & year>2014, discrete freq color(gs12%25) ylab(0.2(0.2)1,nogrid) xlab(-6600 3882 41100) yaxis(2) yscale(alt axis(2)) below legend(off))

logit trust_eu c.gdp_reg_ch_5##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg=(-6600 3882 41100)) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]

sum gdp_reg_ch_5
tab nuts2_region if gdp_reg_ch_5==-6600
tab year if gdp_reg_ch_5==-6600
tab nuts2_region if gdp_reg_ch_5==41100
tab year if gdp_reg_ch_5==41100

//Wealth growth measured as groups (Model 4, Fig B in Appendix)

logit trust_eu ib(2).gdp_reg_gr_ch trust_gov ib(1).ec_exp lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_gr_ch=(1 2 3)) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]
marginsplot, recast(scatter) recastci(rcap) ///
	graphregion(color(white) lwidth(large)) ///
	title("") ///
	xtitle("Regional groups based on GDP 5-year change", margin(small)) ///
	xscale(range(0.5 1 2 3 3.5)) ///
	xlab(1 `""No or little" "improvement""' 2 `""Moderate" "improvement""' 3 `""Large" "improvement""') ///
	ylab(0.3(0.1)0.7,nogrid) ///
	legend(col(1))

//H3: EU trust is likely to be higher among poorer and middle-income European regions that are experiencing growth compared to rich regions that are experiencing growth
//Wealth status and wealth growth interaction, both measured as continuous (Fig 4, Model 5)

logit trust_eu c.gdp_reg##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg=(8100 28805 79600) gdp_reg_ch_5=(-6600(1000)41100)) atmeans post
marginsplot, x(gdp_reg_ch_5) recast(line) recastci(rarea) ///
	graphregion(color(white) lwidth(large)) ///
	plot1opts(lpattern(solid) lcolor(black) lwidth(medium)) ///
		plot2opts(lpattern(shortdash) lcolor(black) lwidth(medium)) ///
		plot3opts(lpattern(longdash_dot) lcolor(black) lwidth(large)) ///		
	ci1opts(lcolor(gs15) fcolor(gs15)) ///
		ci2opts(lcolor(gs12) fcolor(gs12%70)) ///
		ci3opts(lcolor(gs10) fcolor(gs10)) ///
	title ("") ///
	xtitle ("Regional GDP; 5-year change") ///
	ytitle (, margin(medsmall)) ///
	xtitle (, margin(medsmall)) ///
	xlab (-6600 3882 41100) ///
	xline (3882, lcolor(red) lpattern(dash)) ///
	addplot(histogram gdp_reg_ch_5 if nonmiss==1 & year>2014, ///
		discrete freq color(gs12%25) ylab(0.2(0.2)1,nogrid) xlab(-6600 3882 41100) ///
		yaxis(2) yscale(alt axis(2)) ///
		legend(row(1) order(4 "Poorest region" 5 "Mean region" 6 "Wealthiest region")) ///
		legend(size(small))) 

logit trust_eu c.gdp_reg##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg=8100 gdp_reg_ch_5=(-6600 41100)) atmeans post
test _b[1._at] = _b[2._at]
margins, at(gdp_reg=28805 gdp_reg_ch_5=(-6600 41100)) atmeans post
test _b[1._at] = _b[2._at]
margins, at(gdp_reg=79600 gdp_reg_ch_5=(-6600 41100)) atmeans post
test _b[1._at] = _b[2._at]
margins, at(gdp_reg=(8100 28805 79600) gdp_reg_ch_5=-6600) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]
margins, at(gdp_reg=(8100 28805 79600) gdp_reg_ch_5=41100) atmeans post
test _b[1._at] = _b[2._at]
test _b[1._at] = _b[3._at]
test _b[2._at] = _b[3._at]

*Wealth status and wealth growth interaction, both measured as groups (Model 6, Fig C in Appendix)

logit trust_eu ib(4).gdp_reg_gr_ch_wlth trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_gr_ch_wlth==(1 2 3 4 5 6 7 8 9)) atmeans post
test _b[1._at] = _b[3._at]
test _b[4._at] = _b[6._at]
test _b[7._at] = _b[9._at]
marginsplot, recast(scatter) recastci(rcap) ///
	graphregion(color(white) lwidth(large)) ///
	title("") ///
	xtitle("", margin(small)) ///
	xlab(1 `" "Poor & no or little" "improvement" "' 2 `" "Poor & moderate" "improvement" "' 3 `" "Poor & large" "improvement" "' ///
		4 `" "Middle & no or little" "improvement "' 5 `" "Middle & moderate" "improvement" "' 6 `" "Middle & large" "improvement" "' ///
		7 `" "Wealthy & no or little" "improvement" "' 8 `" "Wealthy & moderate" "improvement" "' 9 `" "Wealthy & large" "improvement" "') ///
	xlab(,angle(90)) ///
	ylab(,nogrid) ///
	legend(col(1))
	
margins gdp_reg_gr_ch_wlth, pwcompare post
//If CIs don't include 0, the difference is statistically significant

	
		
*****Robustness tests******

//Growth measured as t-3
//Wealth growth measured as continuous (Fig D in Appendix; robustness test for Fig 3 and Model 3)

logit trust_eu c.gdp_reg_ch_3##c.gdp_reg_ch_3 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_ch_3=(-8300(1000)30000)) atmeans post
marginsplot, recast(line) recastci(rarea) ///
	graphregion(color(white) lwidth(large)) ///
	plot1opts(lpattern(solid) lcolor(black) lwidth(medium)) ///
	ciopts(lcolor(gs10) fcolor(gs10)) ///
	title ("") ///
	xtitle ("Regional GDP; 3-year change") ///
	ytitle (, margin(medsmall)) ///
	xtitle (, margin(medsmall)) ///
	xlab (-8300 1000 30000) ///
	xline (2374, lcolor(red) lpattern(dash)) ///
	addplot(histogram gdp_reg_ch_3 if nonmiss==1 & year>2013, discrete freq color(gs12%25) ylab(0.2(0.2)1,nogrid) xlab(-8300 2374 30000) yaxis(2) yscale(alt axis(2)) below legend(off))

//Growth measured as t-1
//Wealth growth measured as continuous (Fig E in Appendix; robustness test for Fig 3 and Model 3)

oneway gdp_reg_ch_1 year, mean	
sum gdp_reg_ch_1 if year>2011
logit trust_eu c.gdp_reg_ch_1##c.gdp_reg_ch_1 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)
margins, at(gdp_reg_ch_1=(-5700(1000)30000)) atmeans post
marginsplot, recast(line) recastci(rarea) ///
	graphregion(color(white) lwidth(large)) ///
	plot1opts(lpattern(solid) lcolor(black) lwidth(medium)) ///
	ciopts(lcolor(gs10) fcolor(gs10)) ///
	title ("") ///
	xtitle ("Regional GDP; 1-year change") ///
	ytitle (, margin(medsmall)) ///
	xtitle (, margin(medsmall)) ///
	xlab (-5700 1000 30000) ///
	xline (817, lcolor(red) lpattern(dash)) ///
	addplot(histogram gdp_reg_ch_1 if nonmiss==1 & year>2011, discrete freq color(gs12%25) ylab(0.2(0.2)1,nogrid) xlab(-5700 817 30000) yaxis(2) yscale(alt axis(2)) below legend(off))

//Models controlling for attachment to country (as a measure of identity)
//Reduced sample size due to limited availability of the variable in EB surveys

*Model 1
logit trust_eu c.gdp_reg##c.gdp_reg trust_gov ib(1).ec_exp ib(0).attach_cntry c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)

*Model 3
logit trust_eu c.gdp_reg_ch_5##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp ib(0).attach_cntry c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)

*Model 5
logit trust_eu c.gdp_reg##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp ib(0).attach_cntry c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014, cluster(nuts2_region)

//Multilevel models

egen countryyear=group(country year)
tab countryyear

egen regionyear=group(nuts2_region year)
sum regionyear

*Model 1
melogit trust_eu c.gdp_reg##c.gdp_reg trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014 || countryyear: || regionyear:

*Model 3
melogit trust_eu c.gdp_reg_ch_5##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014 || countryyear: || regionyear:

*Model 5
melogit trust_eu c.gdp_reg##c.gdp_reg_ch_5 trust_gov ib(1).ec_exp c.lr c.edu ib(0).occ c.age i.gender i.year c.gdp_nat if nonmiss==1 & year>2014 || countryyear: || regionyear:


	
*****Maps (Fig 1)*****

//Nuts data from https://gisco-services.ec.europa.eu/distribution/v2/nuts/download/#nuts21

spshape2dta "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/NUTS 2016/NUTS_RG_03M_2016_4326_LEVL_1.shp", replace saving(nuts1)
spshape2dta "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/NUTS 2016/NUTS_RG_03M_2016_4326_LEVL_2.shp", replace saving(nuts2)
spshape2dta "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/NUTS 2016/NUTS_RG_03M_2016_4326_LEVL_3.shp", replace saving(nuts3)

use nuts1, clear
spmap using nuts2_shp, id(_ID)

use nuts2, clear
spmap using nuts2_shp, id(_ID)

use nuts3, clear
spmap using nuts2_shp, id(_ID)

//Getting rid of small islands

foreach x in nuts1 nuts2 nuts3 {
  
  use `x'_shp, clear
    keep if _X > -10 & _Y >20        
    geo2xy _Y _X, proj(web_mercator) replace
  save, replace
}

//Trying out the map outline

use nuts2, clear
spmap using nuts2_shp, id(_ID)
spmap _ID using nuts2_shp, id(_ID) cln(5) fcolor(Heat)	
	
spmap _ID using nuts2_shp, ///
 id(_ID) cln(5) fcolor(Heat) ///
 legend(pos(11) size(2.5)) legstyle(2)
 
//Creating data file with trust and gdp variables

use "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/Pooled EB dataset_new_for map.dta", clear

*adding trust and gdp variables

keep if year==2019
keep GDP_2019 trust_eu nuts2_region
decode nuts2_region, gen(NUTS_ID)

replace NUTS_ID="HR04" if NUTS_ID=="HR02" | NUTS_ID=="HR05" | NUTS_ID=="HR06" 

collapse (mean) trust_eu GDP_2019, by(NUTS_ID)

*matching nuts value labels to Eurostat ones

expand 4 if NUTS_ID=="DE1", gen(dupindicator)
replace NUTS_ID="DE11" if NUTS_ID=="DE1" & dupindicator==0
replace NUTS_ID="DE12" if NUTS_ID=="DE1" & dupindicator==1
replace NUTS_ID="DE13" if NUTS_ID[_n-1]=="DE12" & dupindicator==1 
replace NUTS_ID="DE14" if NUTS_ID[_n-1]=="DE13" & dupindicator==1

expand 7 if NUTS_ID=="DE2", gen(dupindicator_1)
replace NUTS_ID="DE21" if NUTS_ID=="DE2" & dupindicator_1==0
replace NUTS_ID="DE22" if NUTS_ID=="DE2" & dupindicator_1==1
replace NUTS_ID="DE23" if NUTS_ID[_n-1]=="DE22" & dupindicator_1==1 
replace NUTS_ID="DE24" if NUTS_ID[_n-1]=="DE23" & dupindicator_1==1 
replace NUTS_ID="DE25" if NUTS_ID[_n-1]=="DE24" & dupindicator_1==1 
replace NUTS_ID="DE26" if NUTS_ID[_n-1]=="DE25" & dupindicator_1==1 
replace NUTS_ID="DE27" if NUTS_ID[_n-1]=="DE26" & dupindicator_1==1 

replace NUTS_ID="DE30" if NUTS_ID=="DE3"	
replace NUTS_ID="DE40" if NUTS_ID=="DE4"	
replace NUTS_ID="DE50" if NUTS_ID=="DE5"	
replace NUTS_ID="DE60" if NUTS_ID=="DE6"	

expand 3 if NUTS_ID=="DE7", gen(dupindicator_2)
replace NUTS_ID="DE71" if NUTS_ID=="DE7" & dupindicator_2==0
replace NUTS_ID="DE72" if NUTS_ID=="DE7" & dupindicator_2==1
replace NUTS_ID="DE73" if NUTS_ID[_n-1]=="DE72" & dupindicator_2==1 

replace NUTS_ID="DE80" if NUTS_ID=="DE8"	

expand 4 if NUTS_ID=="DE9", gen(dupindicator_3)
replace NUTS_ID="DE91" if NUTS_ID=="DE9" & dupindicator_3==0
replace NUTS_ID="DE92" if NUTS_ID=="DE9" & dupindicator_3==1
replace NUTS_ID="DE93" if NUTS_ID[_n-1]=="DE92" & dupindicator_3==1 
replace NUTS_ID="DE94" if NUTS_ID[_n-1]=="DE93" & dupindicator_3==1 

expand 5 if NUTS_ID=="DEA", gen(dupindicator_4)
replace NUTS_ID="DEA1" if NUTS_ID=="DEA" & dupindicator_4==0
replace NUTS_ID="DEA2" if NUTS_ID=="DEA" & dupindicator_4==1
replace NUTS_ID="DEA3" if NUTS_ID[_n-1]=="DEA2" & dupindicator_4==1 
replace NUTS_ID="DEA4" if NUTS_ID[_n-1]=="DEA3" & dupindicator_4==1 
replace NUTS_ID="DEA5" if NUTS_ID[_n-1]=="DEA4" & dupindicator_4==1 

expand 3 if NUTS_ID=="DEB", gen(dupindicator_5)
replace NUTS_ID="DEB1" if NUTS_ID=="DEB" & dupindicator_5==0
replace NUTS_ID="DEB2" if NUTS_ID=="DEB" & dupindicator_5==1
replace NUTS_ID="DEB3" if NUTS_ID[_n-1]=="DEB2" & dupindicator_5==1 

replace NUTS_ID="DEC0" if NUTS_ID=="DEC"	

expand 3 if NUTS_ID=="DED", gen(dupindicator_6)
replace NUTS_ID="DED2" if NUTS_ID=="DED" & dupindicator_6==0
replace NUTS_ID="DED4" if NUTS_ID=="DED" & dupindicator_6==1
replace NUTS_ID="DED5" if NUTS_ID[_n-1]=="DED4" & dupindicator_6==1 

replace NUTS_ID="DEE0" if NUTS_ID=="DEE"	
replace NUTS_ID="DEF0" if NUTS_ID=="DEF"	
replace NUTS_ID="DEG0" if NUTS_ID=="DEG"	

replace NUTS_ID="EL30" if NUTS_ID=="EL30"	//Attiki
replace NUTS_ID="EL43" if NUTS_ID=="EL43"	//Kriti
replace NUTS_ID="EL51" if NUTS_ID=="EL11"	//Anatoliki Makedonia, Thraki
replace NUTS_ID="EL52" if NUTS_ID=="EL12"	//Kentriki Makedonia
replace NUTS_ID="EL53" if NUTS_ID=="EL13"	//Dytiki Makedonia
replace NUTS_ID="EL54" if NUTS_ID=="EL21"	//Ipeiros
replace NUTS_ID="EL61" if NUTS_ID=="EL14"	//Thessalia
replace NUTS_ID="EL63" if NUTS_ID=="EL23"	//Dytiki Ellada
replace NUTS_ID="EL64" if NUTS_ID=="EL24"	//Sterea Ellada
replace NUTS_ID="EL65" if NUTS_ID=="EL25"	//Peloponnisos

expand 2 if NUTS_ID=="HU1", gen(dupindicator_8)
replace NUTS_ID="HU11" if NUTS_ID=="HU1" & dupindicator_8==0
replace NUTS_ID="HU12" if NUTS_ID=="HU1" & dupindicator_8==1

expand 4 if NUTS_ID=="ITC", gen(dupindicator_9)
replace NUTS_ID="ITC1" if NUTS_ID=="ITC" & dupindicator_9==0
replace NUTS_ID="ITC2" if NUTS_ID=="ITC" & dupindicator_9==1
replace NUTS_ID="ITC3" if NUTS_ID[_n-1]=="ITC2" & dupindicator_9==1 
replace NUTS_ID="ITC4" if NUTS_ID[_n-1]=="ITC3" & dupindicator_9==1 

expand 6 if NUTS_ID=="ITF", gen(dupindicator_10)
replace NUTS_ID="ITF1" if NUTS_ID=="ITF" & dupindicator_10==0
replace NUTS_ID="ITF2" if NUTS_ID=="ITF" & dupindicator_10==1
replace NUTS_ID="ITF3" if NUTS_ID[_n-1]=="ITF2" & dupindicator_10==1 
replace NUTS_ID="ITF4" if NUTS_ID[_n-1]=="ITF3" & dupindicator_10==1 
replace NUTS_ID="ITF5" if NUTS_ID[_n-1]=="ITF4" & dupindicator_10==1 
replace NUTS_ID="ITF6" if NUTS_ID[_n-1]=="ITF5" & dupindicator_10==1 

expand 2 if NUTS_ID=="ITG", gen(dupindicator_11)
replace NUTS_ID="ITG1" if NUTS_ID=="ITG" & dupindicator_11==0
replace NUTS_ID="ITG2" if NUTS_ID=="ITG" & dupindicator_11==1

expand 5 if NUTS_ID=="ITH", gen(dupindicator_12)
replace NUTS_ID="ITH1" if NUTS_ID=="ITH" & dupindicator_12==0
replace NUTS_ID="ITH2" if NUTS_ID=="ITH" & dupindicator_12==1
replace NUTS_ID="ITH3" if NUTS_ID[_n-1]=="ITH2" & dupindicator_12==1 
replace NUTS_ID="ITH4" if NUTS_ID[_n-1]=="ITH3" & dupindicator_12==1 
replace NUTS_ID="ITH5" if NUTS_ID[_n-1]=="ITH4" & dupindicator_12==1 

expand 4 if NUTS_ID=="ITI", gen(dupindicator_13)
replace NUTS_ID="ITI1" if NUTS_ID=="ITI" & dupindicator_13==0
replace NUTS_ID="ITI2" if NUTS_ID=="ITI" & dupindicator_13==1
replace NUTS_ID="ITI3" if NUTS_ID[_n-1]=="ITI2" & dupindicator_13==1 
replace NUTS_ID="ITI4" if NUTS_ID[_n-1]=="ITI3" & dupindicator_13==1 

replace NUTS_ID="PL71" if NUTS_ID=="PL11"	//Łódzkie
replace NUTS_ID="PL72" if NUTS_ID=="PL33"	//Świętokrzyskie
replace NUTS_ID="PL81" if NUTS_ID=="PL31"	//Lubelskie
replace NUTS_ID="PL82" if NUTS_ID=="PL32"	//Podkarpackie
replace NUTS_ID="PL84" if NUTS_ID=="PL34"	//Podlaskie

expand 2 if NUTS_ID=="PL12", gen(dupindicator_14)
replace NUTS_ID="PL91" if NUTS_ID=="PL12" & dupindicator_14==0
replace NUTS_ID="PL92" if NUTS_ID=="PL12" & dupindicator_14==1

expand 2 if NUTS_ID=="UKC", gen(dupindicator_15)
replace NUTS_ID="UKC1" if NUTS_ID=="UKC" & dupindicator_15==0
replace NUTS_ID="UKC2" if NUTS_ID=="UKC" & dupindicator_15==1

expand 5 if NUTS_ID=="UKD", gen(dupindicator_16)
replace NUTS_ID="UKD1" if NUTS_ID=="UKD" & dupindicator_16==0
replace NUTS_ID="UKD3" if NUTS_ID=="UKD" & dupindicator_16==1
replace NUTS_ID="UKD4" if NUTS_ID[_n-1]=="UKD3" & dupindicator_16==1 
replace NUTS_ID="UKD6" if NUTS_ID[_n-1]=="UKD4" & dupindicator_16==1 
replace NUTS_ID="UKD7" if NUTS_ID[_n-1]=="UKD6" & dupindicator_16==1 

expand 4 if NUTS_ID=="UKE", gen(dupindicator_17)
replace NUTS_ID="UKE1" if NUTS_ID=="UKE" & dupindicator_17==0
replace NUTS_ID="UKE2" if NUTS_ID=="UKE" & dupindicator_17==1
replace NUTS_ID="UKE3" if NUTS_ID[_n-1]=="UKE2" & dupindicator_17==1 
replace NUTS_ID="UKE4" if NUTS_ID[_n-1]=="UKE3" & dupindicator_17==1 

expand 3 if NUTS_ID=="UKF", gen(dupindicator_18)
replace NUTS_ID="UKF1" if NUTS_ID=="UKF" & dupindicator_18==0
replace NUTS_ID="UKF2" if NUTS_ID=="UKF" & dupindicator_18==1
replace NUTS_ID="UKF3" if NUTS_ID[_n-1]=="UKF2" & dupindicator_18==1 

expand 3 if NUTS_ID=="UKG", gen(dupindicator_19)
replace NUTS_ID="UKG1" if NUTS_ID=="UKG" & dupindicator_19==0
replace NUTS_ID="UKG2" if NUTS_ID=="UKG" & dupindicator_19==1
replace NUTS_ID="UKG3" if NUTS_ID[_n-1]=="UKG2" & dupindicator_19==1 

expand 3 if NUTS_ID=="UKH", gen(dupindicator_20)
replace NUTS_ID="UKH1" if NUTS_ID=="UKH" & dupindicator_20==0
replace NUTS_ID="UKH2" if NUTS_ID=="UKH" & dupindicator_20==1
replace NUTS_ID="UKH3" if NUTS_ID[_n-1]=="UKH2" & dupindicator_20==1 

expand 5 if NUTS_ID=="UKI", gen(dupindicator_21)
replace NUTS_ID="UKI3" if NUTS_ID=="UKI" & dupindicator_21==0
replace NUTS_ID="UKI4" if NUTS_ID=="UKI" & dupindicator_21==1
replace NUTS_ID="UKI5" if NUTS_ID[_n-1]=="UKI4" & dupindicator_21==1 
replace NUTS_ID="UKI6" if NUTS_ID[_n-1]=="UKI5" & dupindicator_21==1 
replace NUTS_ID="UKI7" if NUTS_ID[_n-1]=="UKI6" & dupindicator_21==1 

expand 4 if NUTS_ID=="UKJ", gen(dupindicator_22)
replace NUTS_ID="UKJ1" if NUTS_ID=="UKJ" & dupindicator_22==0
replace NUTS_ID="UKJ2" if NUTS_ID=="UKJ" & dupindicator_22==1
replace NUTS_ID="UKJ3" if NUTS_ID[_n-1]=="UKJ2" & dupindicator_22==1 
replace NUTS_ID="UKJ4" if NUTS_ID[_n-1]=="UKJ3" & dupindicator_22==1 

expand 4 if NUTS_ID=="UKK", gen(dupindicator_23)
replace NUTS_ID="UKK1" if NUTS_ID=="UKK" & dupindicator_23==0
replace NUTS_ID="UKK2" if NUTS_ID=="UKK" & dupindicator_23==1
replace NUTS_ID="UKK3" if NUTS_ID[_n-1]=="UKK2" & dupindicator_23==1 
replace NUTS_ID="UKK4" if NUTS_ID[_n-1]=="UKK3" & dupindicator_23==1 

expand 2 if NUTS_ID=="UKL", gen(dupindicator_24)
replace NUTS_ID="UKL1" if NUTS_ID=="UKL" & dupindicator_24==0
replace NUTS_ID="UKL2" if NUTS_ID=="UKL" & dupindicator_24==1

expand 5 if NUTS_ID=="UKM", gen(dupindicator_25)
replace NUTS_ID="UKM5" if NUTS_ID=="UKM" & dupindicator_25==0
replace NUTS_ID="UKM6" if NUTS_ID=="UKM" & dupindicator_25==1
replace NUTS_ID="UKM7" if NUTS_ID[_n-1]=="UKM6" & dupindicator_25==1 
replace NUTS_ID="UKM8" if NUTS_ID[_n-1]=="UKM7" & dupindicator_25==1 
replace NUTS_ID="UKM9" if NUTS_ID[_n-1]=="UKM8" & dupindicator_25==1 

replace NUTS_ID="UKN0" if NUTS_ID=="UKN"

drop dup*

save "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/nuts for merge.dta", replace

//Merging the nuts file with the data file 

use nuts2, clear
merge 1:1 NUTS_ID using "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/nuts for merge.dta"

keep if _merge==3

save "/Users/Liisa/Dropbox/york/Liisa/Political geography of discontent/Data/nuts and data merged.dta", replace

//Creating maps

encode NUTS_ID, gen(ID)

spmap trust_eu using nuts2_shp, ///
 id(_ID) cln(5) fcolor(Heat) ///
 ocolor(gs2 ..) osize(0.03 ..) ///
 legend(pos(11) size(2.5))  legstyle(2) 
 
spmap GDP_2019 using nuts2_shp, ///
 id(_ID) cln(5) clm(k) fcolor(Heat) ///
 ocolor(gs2 ..) osize(0.03 ..) ///
 legend(pos(11) size(2.5))  legstyle(2) 
*k-mean clusters used for cut-offs
 